//
//  SettingViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/28.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "SettingViewController.h"
#import "FeedbackViewController.h"
#import "UpdatePwdViewController.h"
#import "AboutOurViewController.h"
#import "UIWebViewController.h"
#import "Singleton.h"
#import "TSHeader.h"
#import "CustomAlertView.h"
#import "JKDBHelper.h"
#import "SDImageCache.h"
#import "ComMethod.h"

@interface SettingViewController ()<CustomAlertViewDelegate> {
}

@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"设置";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    } else if (section == 1) {
        return 2;
    } else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"SettingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        if (indexPath.section != 2) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIndentifier];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:11];
            cell.detailTextLabel.textColor = AssistTextColor;
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(190, 12, 100, 20)];
            lable.backgroundColor = [UIColor clearColor];
            lable.font = [UIFont systemFontOfSize:13];
            lable.textColor = OrangeColor;
            lable.textAlignment = NSTextAlignmentRight;
            lable.tag = 105;
            [cell.contentView addSubview:lable];
        } else {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
            cell.detailTextLabel.textColor = OrangeColor;
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = MainTextColor;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"修改密码";
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"意见反馈";
        } else if (indexPath.row == 2) {
            UILabel *label = (UILabel *)[cell viewWithTag:105];
            cell.textLabel.text = @"清除缓存";
            cell.detailTextLabel.text = @"清除应用产生的图片、缓存数据等";
            label.text = [self caculateCacheData];
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"规范守则";
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"关于我们";
        }
    } else if (indexPath.section == 2) {
        cell.textLabel.text = @"退出登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textColor = [UIColor redColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [MobClick event:@"shezhi_btn_xiugaimima"];
            UpdatePwdViewController *updatePVC = [[UpdatePwdViewController alloc] initWithNibName:@"UpdatePwdViewController" bundle:nil];
            [self.navigationController pushViewController:updatePVC animated:YES];
        } else if (indexPath.row == 1) {
            [MobClick event:@"shezhi_btn_yijianfankui"];
            FeedbackViewController *feedbackVC = [[FeedbackViewController alloc] initWithNibName:@"FeedbackViewController" bundle:nil];
            [self.navigationController pushViewController:feedbackVC animated:YES];
        } else if (indexPath.row == 2) {
            [MobClick event:@"shezhi_btn_qingchuhuancun"];
            CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:nil message:@"确认要清除缓存数据吗？" delegate:self cancelButtonTitle:@"取消" sureButtonTitle:@"确认"];
            [alert show];
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [MobClick event:@"shezhi_btn_guifanshouze"];
            UIWebViewController *webVC = [[UIWebViewController alloc] initWithNibName:@"UIWebViewController" bundle:nil];
            webVC.title = @"规范守则";
            webVC.webUrl = [NSString stringWithFormat:@"%@/documents/tyxz_privacy.html",[Singleton shareInstance].cityModel.manageSystemUrl];
            [self.navigationController pushViewController:webVC animated:YES];
        } else if (indexPath.row == 1) {
            [MobClick event:@"shezhi_btn_guanyuwomen"];
            AboutOurViewController *aboutOVC = [[AboutOurViewController alloc] initWithNibName:@"AboutOurViewController" bundle:nil];
            [self.navigationController pushViewController:aboutOVC animated:YES];
        }
    } else if (indexPath.section == 2) {
        [MobClick event:@"shezhi_btn_tuichudenglu"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LogoutSuccess" object:nil];
    }
}

//计算内存数据
- (NSString *)caculateCacheData {
    NSInteger imageSize = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = imageSize/1024.0/1024.0;
    return [NSString stringWithFormat:@"%.1fM",cacheSize];
}

#pragma mark CustomAlertViewDelegate
- (void)customAlertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
        [ComMethod clearJKDBModelData];
        [self.tableView reloadData];
    }
}

@end
